package com.odl.ds.process;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author oudl
 * @date 2025/08/06 11:12
 */
@Slf4j
public class UndrawProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    @Override
    public void process(Page page) {
        String pageText=page.getHtml().toString();
        //URL匹配
        String pattern = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(pageText);
        List<String> links=new ArrayList<>();
        while (m.find()){
            String link=m.group();
            if(link.contains("https://cdn.undraw.co/illustration")){
                log.info(link);
                links.add(link);
            }
        }
        page.putField("data",links);
    }

    @Override
    public Site getSite() {
        return site;
    }

}

@Getter
class UndrawEntity{
    public String url;
    public String name;
}